package com.lantosec.hmrp.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.system.entity.SysDictionaries;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import com.lantosec.hmrp.modules.system.vo.SysDictionariesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 数据字典表 控制器
 *
 * @author AutoCode
 * @since 2020-02-11
 */
@Api(value = "数据字典表控制器", tags = "数据字典表控制器")
@RestController
@RequestMapping("/system/dictionaries")
@Log4j2
public class SysDictionariesController extends BaseController {
    @Autowired
    private ISysDictionariesService service;


    @PostMapping("insertDictionariesInfo")
    @ApiOperation(value = "新增数据")
    public Result<String> insertDictionariesInfo(@RequestBody @Validated SysDictionariesVO vo, BindingResult bindingResult) {
        validationHandler(bindingResult);
        SysDictionaries entity = vo.convertToEntity();
        //新增数据前先查询当前名字是否存在，存在即更新当前名字的数据
        String dictionariesName = entity.getDictionariesName();
        List list = service.getDictionariesByName(dictionariesName);
        if(list.size()<1){
            String maxId = service.queryMaxId(entity.getParentId());
            entity.setDictionariesId(maxId);
            entity.setLeaf(0);
            entity.setAllowDel(1);
            service.save(entity);
            return Result.ok(entity.getDictionariesId());

        }else {
            service.updateDictionariesByName(entity);
            return Result.ok();
        }
    }

    @PutMapping("updateDictionariesInfo")
    @ApiOperation(value = "修改数据")
    public Result<Void> updateDictionariesInfo(@RequestBody SysDictionariesVO vo) {
        SysDictionaries entity = vo.convertToEntity();
        Boolean update = service.updateById(entity);
        if(update){
            return Result.ok();
        }else {
            return Result.fail("操作失败");
        }
    }

        @DeleteMapping(value = "deleteDictionariesInfo/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> deleteDictionariesInfo(@PathVariable(name = "ids") String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            Boolean delete = service.removeByIds(Arrays.asList(ids.split(",")));
            if(!delete){
                return Result.fail("操作失败");
            }
        }
        return Result.ok();
    }

    @GetMapping("getDictionariesTree")
    @ApiOperation(value = "获得字典树")
    public Result<List<TreeNodeVo>> getDictionariesTree() {
        return Result.ok(service.dictionariesTree());
    }

    @GetMapping("getDictionariesTree/{id}")
    @ApiOperation(value = "根据指定ID查询对应的字典树")
    public Result<List<TreeNodeVo>> getDictionariesTree(@PathVariable(name = "id") String id) {
        return Result.ok(service.dictionariesTree(id));
    }


    @PutMapping("updateOrder")
    @ApiOperation(value = "更新字典数据排序")
    public Result<Void> updateOrder(@ApiParam(value = "字典ID", required = true) @RequestParam(name = "id") String id,
                                    @ApiParam(value = "排序值", required = true) @RequestParam(name = "dictionariesOrder") Integer dictionariesOrder) {
        service.updateOrder(id,dictionariesOrder);
        return Result.ok();
    }

    @GetMapping("getDictionariesById")
    @ApiOperation(value = "查询当前数据字典下的子数据")
    public Result<List<SysDictionariesVO>> getDictionariesById(@ApiParam(value = "ID", required = true) @RequestParam(name="id") String id){
        List<SysDictionaries> sysDictionariesVOS = service.getDictionariesById(id);
        return Result.ok(entity2VO(sysDictionariesVOS));
    }

}
